package com.sven.media.utils;

import android.media.MediaRecorder;

import java.io.File;
import java.io.IOException;

/**
 * 录音工具类
 */
public class RecordUtil {

    private static final int SAMPLE_RATE_IN_HZ = 8000;
    private MediaRecorder recorder;
    private String mPath;

    public RecordUtil(String path) {
        mPath = path;
    }

    /*开始录音*/
    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted,It is  " + state
                    + ".");
        }
        File directory = new File(mPath).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created");
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//		recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// AAC_ADTS格式在API级别16加入
//		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);//
        recorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ);
        recorder.setOutputFile(mPath);
        recorder.prepare();
        recorder.start();
    }

    /*结束录音*/
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
        recorder = null;
    }

    /*获取录音时间*/
    public double getAmplitude() {
        if (recorder != null) {
            return (recorder.getMaxAmplitude());
        }
        return 0;
    }
}
